package com.wan.dao.user;

import com.mysql.jdbc.StringUtils;
import com.wan.dao.BaseDao;
import com.wan.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao{

    @Override
    public User getLoginUser(Connection conn, String userCode) {

        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;

        if(conn != null) {
            try {
                String sql = "select * from smbms_user where userCode=?";
                Object[] params = {userCode};

                rs = BaseDao.execute(conn,pstm,rs,sql,params);

                if(rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUserCode(rs.getString("userCode"));
                    user.setUserName(rs.getString("userName"));
                    user.setUserPassword(rs.getString("userPassword"));
                    user.setGender(rs.getInt("gender"));
                    user.setBirthday(rs.getDate("birthday"));
                    user.setPhone(rs.getString("phone"));
                    user.setAddress(rs.getString("address"));
                    user.setUserRole(rs.getInt("userRole"));
                    user.setCreatedBy(rs.getInt("createdBy"));
                    user.setCreationDate(rs.getDate("creationDate"));
                    user.setModifyBy(rs.getInt("modifyBy"));
                    user.setModifyDate(rs.getDate("modifyDate"));
                }
                BaseDao.closeResource(null,pstm,rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return user;
    }

    @Override
    public int updatePwd(Connection conn, int id, String pwd) {
//        System.out.println(pwd);
        PreparedStatement pstm = null;
        int impactCount = 0;
        String sql = "update smbms_user set userPassword = ? where id = ?";
        Object[] params = {pwd,id};
        try {
            impactCount = BaseDao.executeUpdate(conn, pstm, params, sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return impactCount;
    }

    @Override
    public int getUserCount(Connection conn, String userName, int userRole) {

        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = null;
        ArrayList<Object> list = null;
        int count = 0;

        if(conn != null) {
            sql = new StringBuffer();
            // 新建一个list存储参数的具体形式，像模糊查询这种还需要添加额外的符号
            list = new ArrayList<Object>();
            sql.append("select count(1) AS count from smbms_user u,smbms_role r where u.userRole = r.id");
            if(userRole > 0 ) {
                sql.append(" and r.id = ?");
                list.add(userRole);
            }
            if(userName != null) {
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");
            }
            System.out.println("UserDaoImpl---getUserCount--->"+sql.toString());
        }

        Object[] params = list.toArray();
        try {
            rs = BaseDao.execute(conn, pstm, rs, sql.toString(), params);

            if(rs.next()) {
                 count = rs.getInt("count");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(null,pstm,rs);
        }
        System.out.println("查询到的count为："+count);

        return count;
    }

    @Override
    public List<User> getUserList(Connection conn, String userName, int userRole, int currentPageNo, int pageSize) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = new StringBuffer();
        //存储User的list
        ArrayList<User> usersList = new ArrayList<User>();

        if (conn != null) {
            sql.append("select u.*,r.roleName AS userRoleName  from smbms_user u,smbms_role r where u.userRole = r.id");
            // 存储参数的list
            ArrayList<Object> list = new ArrayList<Object>();
            if(!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");
            }
            if(userRole > 0) {
                sql.append(" and userRole = ?");
                list.add(userRole);
            }
            //根据输入的当前页码和页面大小计算分页
            sql.append(" order by creationDate DESC limit ?,?");
            currentPageNo = (currentPageNo - 1)*pageSize;
            list.add(currentPageNo);
            list.add(pageSize);

            System.out.println("userDaoImpl---getUserList--->"+sql.toString());

            Object[] params = list.toArray();
            try {
                rs = BaseDao.execute(conn, pstm, rs, sql.toString(), params);
                while(rs.next()){
                    User _user = new User();
                    _user.setId(rs.getInt("id"));
                    _user.setUserCode(rs.getString("userCode"));
                    _user.setUserName(rs.getString("userName"));
                    _user.setUserPassword(rs.getString("userPassword"));
                    _user.setGender(rs.getInt("gender"));
                    _user.setBirthday(rs.getDate("birthday"));
                    _user.setPhone(rs.getString("phone"));
                    _user.setAddress(rs.getString("address"));
                    _user.setUserRole(rs.getInt("userRole"));
                    _user.setCreatedBy(rs.getInt("createdBy"));
                    _user.setCreationDate(rs.getDate("creationDate"));
                    _user.setModifyBy(rs.getInt("modifyBy"));
                    _user.setModifyDate(rs.getDate("modifyDate"));
                    _user.setUserRoleName(rs.getString("userRoleName"));
                    usersList.add(_user);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                BaseDao.closeResource(null,pstm,rs);
            }
        }
        return usersList;
    }

    @Override
    public int addUser(Connection conn, User user) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = null;
        ArrayList<Object> list = null;
        int count = 0;

        if(conn != null) {
            sql = new StringBuffer();
            // 使用list存储穿过来的参数
            list = new ArrayList<Object>();
            sql.append("insert into smbms_user(" +
                    "userCode,userName,userPassword,gender," +
                    "birthday,phone,address,userRole,createdBy," +
                    "creationDate) values(?,?,?,?,?,?,?,?,?,?)");

            list.add(user.getUserCode());
            list.add(user.getUserName());
            list.add(user.getUserPassword());
            list.add(user.getGender());
            list.add(user.getBirthday());
            list.add(user.getPhone());
            list.add(user.getAddress());
            list.add(user.getUserRole());
            list.add(user.getCreatedBy());
            list.add(user.getCreationDate());

            System.out.println("UserDaoImpl-->addUser-->"+sql.toString()+list);
            try {
                count = BaseDao.executeUpdate(conn, pstm, list.toArray(), sql.toString());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                BaseDao.closeResource(null,pstm,rs);
            }
        }
        return count;
    }

    @Override
    public int userExist(Connection conn, String userCode) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = null;
        ArrayList<Object> list = null;
        int count = 0;

        if(conn != null) {
            sql = new StringBuffer();
            // 新建一个list存储参数的具体形式，像模糊查询这种还需要添加额外的符号
            list = new ArrayList<Object>();
            sql.append("select count(1) AS count from smbms_user u where ");

            if(userCode != null ) {
                sql.append("  u.userCode = ?");
                list.add(userCode);
            }
        }
        Object[] params = list.toArray();
        try {
            rs = BaseDao.execute(conn, pstm, rs, sql.toString(), params);

            if(rs.next()) {
                count = rs.getInt("count");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(null,pstm,rs);
        }
        return count;
    }

    @Override
    public int deleteUserById(Connection conn, int id) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = null;
        ArrayList<Object> list = null;
        int count = 0;

        if(conn != null) {
            sql = new StringBuffer();
            // 新建一个list存储参数的具体形式，像模糊查询这种还需要添加额外的符号
            list = new ArrayList<Object>();
            sql.append("delete from smbms_user where ");

            if(id != -1) {
                sql.append(" id = ? ");
                list.add(id);
            }
        }
        Object[] params = list.toArray();
        try {
            count = BaseDao.executeUpdate(conn, pstm,  params, sql.toString());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(null,pstm,rs);
        }
        return count;
    }

    @Override
    public int updateUser(Connection conn, User user,int modifyBy) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = null;
        ArrayList<Object> list = null;
        int count = 0;

        if(conn != null) {
            sql = new StringBuffer();
            // 新建一个list存储参数的具体形式，像模糊查询这种还需要添加额外的符号
            list = new ArrayList<Object>();
            sql.append("update smbms_user set ");

            if (user.getUserName() != null) {
                sql.append(" userName = ? ,");
                list.add(user.getUserName());
            }
            if (user.getGender() != null) {
                sql.append(" gender = ? ,");
                list.add(user.getGender());
            }
            if (user.getBirthday() != null) {
                sql.append(" birthday = ? ,");
                list.add(user.getBirthday());
            }
            if (user.getPhone() != null) {
                sql.append(" phone = ? ,");
                list.add(user.getPhone());
            }
            if (user.getAddress() != null) {
                sql.append(" address = ? ,");
                list.add(user.getAddress());
            }
            if (user.getUserRole() != null) {
                sql.append(" userRole = ? ,");
                list.add(user.getUserRole());
            }
            sql.append(" modifyBy = ?,");
            list.add(modifyBy);
            sql.append(" modifyDate = ?");
            list.add(user.getModifyDate());
            sql.append(" where id = ?");
            System.out.println("id---->"+user.getId());
            list.add(user.getId());
        }
        System.out.println("list--->"+list);
        System.out.println(sql.toString());
        Object[] params = list.toArray();
        try {
            count = BaseDao.executeUpdate(conn, pstm, params, sql.toString());
            System.out.println("count--->"+count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(null,pstm,rs);
        }
        return count;
    }

    @Override
    public User getUserById(Connection conn, int id) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        StringBuffer sql = null;
        ArrayList<Object> list = null;
        User _user = null;
        if(conn != null) {
            sql = new StringBuffer();
            // 新建一个list存储参数的具体形式，像模糊查询这种还需要添加额外的符号
            list = new ArrayList<Object>();
            sql.append("select u.*,r.roleName AS userRoleName from smbms_user u, smbms_role r where u.userRole = r.id and");

            if (id != -1) {
                sql.append(" u.id = ?");
                list.add(id);
            }
        }
        Object[] params = list.toArray();
        try {
            rs = BaseDao.execute(conn,pstm,rs,sql.toString(), params);
            while (rs.next()) {
                _user = new User();
                _user.setId(rs.getInt("id"));
                _user.setUserCode(rs.getString("userCode"));
                _user.setUserName(rs.getString("userName"));
                _user.setUserPassword(rs.getString("userPassword"));
                _user.setGender(rs.getInt("gender"));
                _user.setBirthday(rs.getDate("birthday"));
                _user.setPhone(rs.getString("phone"));
                _user.setAddress(rs.getString("address"));
                _user.setUserRole(rs.getInt("userRole"));
                _user.setCreatedBy(rs.getInt("createdBy"));
                _user.setCreationDate(rs.getDate("creationDate"));
                _user.setModifyBy(rs.getInt("modifyBy"));
                _user.setModifyDate(rs.getDate("modifyDate"));
                _user.setUserRoleName(rs.getString("userRoleName"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(null,pstm,rs);
        }
        return _user;
    }
}
